package fr.mywiki.view.web.struts.form.structure;

import java.rmi.RemoteException;
import java.util.Locale;

import javax.ejb.FinderException;
import javax.servlet.http.HttpServletRequest;

import fr.mywiki.business.resources.ResourceManager;
import fr.mywiki.business.structure.StructureLite;

/**
 * This component contains all the data useful for single structure selection
 * inside a jsp. Becomes abstract and must be extended by
 * SelectSingleStructController or SelectMultiStructController.
 */
public abstract class SelectStructureController {
	/** The complete tree to display in which the structure has to be selected. */
	private String tree;
	
	/** The name of the root of the tree */
	private String rootName;
	
	/**
	 * The Javascript code which initializes the branches shown or not in the tree
	 */
	private String expandJS;
	
	/** Location of the image tree.closed */
	private String rootImage;
	
	public String getRootName() {
		return rootName;
	}

	public String getTree() {
		return tree;
	}

	public void setRootName(String rootName) {
		this.rootName = rootName;
	}

	public void setTree(String tree) {
		this.tree = tree;
	}

	public String getExpandJS() {
		return expandJS;
	}

	public void setExpandJS(String expandJS) {
		this.expandJS = expandJS;
	}

	public String getRootImage() {
		return rootImage;
	}

	public void setRootImage(String rootImage) {
		this.rootImage = rootImage;
	}
	
	/**
	 * Initializes the tree.
	 * 
	 * @param request
	 *          the current request is used to determine which language to use in
	 *          the tree.
	 */
	public void initTree(HttpServletRequest request) throws RemoteException,
			FinderException {
		// root name
		setRootName(ResourceManager.getMessage("structure.root.name",
				ResourceManager.getLocale(request)));

		// tree structure
		setTree(loadTree(ResourceManager.getLocale(request)));

		centerTree(null);

		setRootImage(ResourceManager.getImage("tree.closed",
				ResourceManager.getLocale(request)));
	}
	
	public abstract void centerTree(StructureLite structure)
			throws RemoteException, FinderException;
	
	/** Returns the type of the controller. */
	public abstract String getControllerType();
	
	/**
	 * Loads the whole tree.
	 * 
	 * @param locale the current locale
	 * @return the html string description of the tree.
	 */
	public abstract String loadTree(Locale locale) throws RemoteException, FinderException;
}
